package com.tumanex.socialshare;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 
 * @author artemka
 * 
 */
public class ParseResponse {

	public final static String TAG_ID = "id";
	public final static String TAG_PID = "pid";
	public final static String TAG_AID = "aid";
	public final static String TAG_OWNER_ID = "owner_id";
	public final static String TAG_SRC = "src";
	public final static String TAG_SRC_BIG = "src_big";
	public final static String TAG_SRC_SMALL = "src_small";
	public final static String TAG_CREATED = "created";

	public final static String TAG_SERVER = "server";
	public final static String TAG_PHOTO = "photo";
	public final static String TAG_HASH = "hash";

	public final static String TAG_RESPONSE = "response";

	public final static String TAG_POST_ID = "post_id";

	public final static String TAG_UPLOAD_URL = "upload_url";

	public HashMap<String, String> params;

	public ParseResponse() {

		params = new HashMap<String, String>();
	}

	public HashMap<String, String> parseVkWallUploadServer(JSONObject jsonObj)
			throws JSONException {

		JSONObject obj = jsonObj.getJSONObject(TAG_RESPONSE);
		params.put(TAG_UPLOAD_URL, obj.getString(TAG_UPLOAD_URL));

		return params;

	}

	public HashMap<String, String> parseVkSaveWallPhoto(JSONObject jsonObj)
			throws JSONException {

		JSONObject obj = jsonObj.getJSONArray(TAG_RESPONSE).getJSONObject(0);

		params.put(TAG_ID, obj.getString(TAG_ID));
		params.put(TAG_PID, obj.getString(TAG_PID));
		params.put(TAG_AID, obj.getString(TAG_AID));
		params.put(TAG_OWNER_ID, obj.getString(TAG_OWNER_ID));
		params.put(TAG_SRC, obj.getString(TAG_SRC));
		params.put(TAG_SRC_BIG, obj.getString(TAG_SRC_BIG));
		params.put(TAG_SRC_SMALL, obj.getString(TAG_SRC_SMALL));
		params.put(TAG_CREATED, obj.getString(TAG_CREATED));

		return params;

	}

	public HashMap<String, String> parseVkWallPost(JSONObject jsonObj)
			throws JSONException {

		JSONObject obj = jsonObj.getJSONObject(TAG_RESPONSE);
		params.put(TAG_POST_ID, obj.getString(TAG_POST_ID));

		return params;

	}

	public HashMap<String, String> parseExecuteMultipartPost(JSONObject jsonObj)
			throws JSONException {

		params.put(TAG_SERVER, jsonObj.getString(TAG_SERVER));
		params.put(TAG_PHOTO, jsonObj.getString(TAG_PHOTO));
		params.put(TAG_HASH, jsonObj.getString(TAG_HASH));

		return params;

	}

	public HashMap<String, String> getParseParams() {

		return params;

	}

	public int parseErrorCode(JSONObject jsonObj) {

		int errCode = -1;

		if (jsonObj.has("error")) {

			try {
				JSONObject errorObj = jsonObj.getJSONObject("error");
				errCode = errorObj.getInt("error_code");

			} catch (JSONException e) {
			}

		}

		return errCode;

	}

}